Skip to content

HTTP POST请求 - HttpPost

函数简介

发送简单的HTTP POST请求,返回响应体字符串。

接口名称

HttpPost

DLL调用

c
const char* HttpPost(int64_t instance, const char* url, const char* body, const char* content_type);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
url字符串完整URL
body字符串请求体内容
content_type字符串Content-Type,例如"application/json"

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
string resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json");
csharp
using OLAPlug;

var ola = new OLAPlugServer();
string resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json");
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json")
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json");
cpp
var ola = com("OlaPlug.OlaSoft")
var resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json")
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json")
text
.局部变量 ola, OLAPlug
ola.创建 ()
resp = ola.HttpPost(“https://httpbin.org/post“, “{\“name\“:\“ola\“}“, “application/json“)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json");
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
文本型 resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json")
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
string resp = ola.HttpPost("https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json");

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long respPtr = HttpPost(instance, "https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json");
if (respPtr != 0) {
    char resp[512] = {0};
    GetStringFromPtr(respPtr, resp, sizeof(resp));
    FreeStringPtr(respPtr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
long respPtr = HttpPost(instance, "https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json");
if (respPtr != 0) {
    StringBuilder resp = new StringBuilder(GetStringSize(respPtr) + 1);
    GetStringFromPtr(respPtr, resp, resp.Capacity);
    FreeStringPtr(respPtr);
    string respStr = resp.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
respPtr = HttpPost(instance, "https://httpbin.org/post", "{\"name\":\"ola\"}", "application/json")
if respPtr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(respPtr, buf, 512)
    ola.FreeStringPtr(respPtr)
    resp = buf.value.decode("utf-8")

返回值

字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。

注意事项

  • 返回的字符串需要调用 FreeStringPtr 释放内存
  • content_type必须与body内容匹配
  • 常用Content-Type:application/jsonapplication/x-www-form-urlencodedapplication/xmltext/plain
  • 如需自定义请求头或Cookie,请使用 HttpRequestEx